package cn.com.dcsgo.validation.validator;

import cn.com.dcsgo.validation.NotBlankIfNotNull;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.util.StringUtils;

/**
 * NotBlankIfNotNull校验器
 *
 * @author Dcsgo
 * @since 2025/10/1 10:34
 */

public class NotBlankIfNotNullValidator implements ConstraintValidator<NotBlankIfNotNull, String> {
    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        // 如果值为null，校验通过
        if (s == null) {
            return true;
        }
        // 如果值不为null，检查是否为空白字符串
        return StringUtils.hasText(s);
    }
}
